package com.oracle.lingshangejia.fragment;


import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.MyApp;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.activity.ConsultationRecordDetailActivity;
import com.oracle.lingshangejia.activity.LoginActivity;
import com.oracle.lingshangejia.adapter.ConsultationRecordAdapter;
import com.oracle.lingshangejia.bean.ConsultationBean;
import com.oracle.lingshangejia.model.OkgoNetwork;
import com.oracle.lingshangejia.mvp.base.BaseFragment;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.ObserverOnNext;
import com.oracle.lingshangejia.utils.RxUtils;
import com.oracle.lingshangejia.utils.SpUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @松 on 2019/6/26 09:55
 * 专家会诊记录
 */
public class ConsultationFragmentRecord extends BaseFragment implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.rv)
    RecyclerView mRv;
    @BindView(R.id.srl)
    SmartRefreshLayout mRefreshLayout;
    Unbinder unbinder;
    private ConsultationRecordAdapter mConsultationAdapter;
    private List<ConsultationBean.DataBean> mDataBean ;

    @Override
    public int setLayoutId() {
        return R.layout.fragment_consultaion_record;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        unbinder = ButterKnife.bind(this, view);
    }

    @Override
    protected void initData() {
        super.initData();
        initRecyclerView();
        initRefresh();





    }

    @Override
    public void onResume() {
        super.onResume();
        getDate();
    }



    private void initRefresh(){
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getDate();
            }
        });
        mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mRefreshLayout.finishLoadmore();
            }
        });
    }



    private void initRecyclerView(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRv.setLayoutManager(linearLayoutManager);
        mConsultationAdapter = new ConsultationRecordAdapter(mDataBean);
        mRv.setAdapter(mConsultationAdapter);
        mConsultationAdapter.setOnItemClickListener(this);

    }
    private void getDate(){
        int id = (int )SpUtils.get(getContext(),"userid" , 0);
        HashMap<Object,Object> map = new HashMap<>();
        map.put("zrys_id", id);
        map.put("status",2 );
        OkgoNetwork.post(getContext(), Constant.consultation, map)
                .compose(RxUtils.io_main())
                .subscribe(new ObserverOnNext<String>() {
                    @Override
                    public void onNext(String s) {
                        Gson gson = new Gson();
                        ConsultationBean consultationBean = gson.fromJson(s, ConsultationBean.class);
                        mDataBean =consultationBean.getData();
                        if (consultationBean.getMsg().equals("Token已过期")){
                            startActivity(new Intent(getContext(), LoginActivity.class));
                            MyApp.logout();
                            getActivity().finish();
                        }else {
                            if (consultationBean.getCode() == 0) {
                                mConsultationAdapter.replaceData(consultationBean.getData());
                                mRefreshLayout.finishRefresh();

                            } else {
                                MyToast.show("连接服务器失败");
                            }

                        }



                    }
                });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(getActivity(),ConsultationRecordDetailActivity.class);
        intent.putExtra("id",mDataBean.get(position).getId() );//接诊id
        startActivity(intent);

    }
}
